package com.studysystem.repository;

import com.studysystem.entity.DiscussionPost;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface DiscussionPostRepository extends JpaRepository<DiscussionPost, Long> {
    List<DiscussionPost> findByCourseId(Long courseId);
    List<DiscussionPost> findByUserId(Long userId);
    
    @Query("SELECT p FROM DiscussionPost p WHERE p.course.id = ?1 AND p.isPinned = true")
    List<DiscussionPost> findPinnedPostsByCourseId(Long courseId);
    
    @Query("SELECT p FROM DiscussionPost p WHERE p.course.id = ?1 AND p.title LIKE %?2% OR p.content LIKE %?2%")
    Page<DiscussionPost> searchPostsByCourseIdAndKeyword(Long courseId, String keyword, Pageable pageable);
    
    @Query("SELECT COUNT(c) FROM DiscussionComment c WHERE c.post.id = ?1")
    Long getCommentCountByPostId(Long postId);
} 